import pymysql
import requests
'lol热门英雄爬取（功能11）'


# 接口爬虫
def getInterData():
    # lol区
    url = "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=1&tagAll=0&page="
    # 页码
    page = 1
    # 爬取的数据
    result = []
    # 爬取9页够了，不需要太多
    while True:
        # 获取接口返回数据
        res = requests.get(url + str(page))
        # 转成json格式
        response = res.json()
        # 获取详细内容
        artists = response['data']['datas']
        # 如果返回的为空，就说明爬取完了，退出函数
        if len(artists) == 0:
            break
        result.extend(artists)
        print("第%s页" % page)
        page += 1
        # 防止ip被禁用，限制5秒爬一次
        # time.sleep(5)
    return result


# 获取英雄
def getHero(artists, dic):
    for i in artists:
        # 查看attribute字段是否为空
        if i["attribute"] is None:
            continue
        # 不为空，查看ListPos2（key）是否在attribute内存在
        if "ListPos2" in i["attribute"].keys():
            # 英雄不为空
            if i["attribute"]["ListPos2"]["sContent"] != "":
                heroName = i["attribute"]["ListPos2"]["sContent"]
                # 利用字典进行处理
                if heroName not in dic:
                    dic[heroName] = 1
                else:
                    dic[heroName] = dic[heroName] + 1
    return dic


# 创建MySQL连接函数（当做固定）
def getMysqlConn():
    conn = pymysql.connect(host='106.13.194.7',
                           port=3306,
                           user='rdsroot',
                           password='abc123456',
                           db='huya',
                           charset='utf8')
    # 创建操作的游标
    cursor = conn.cursor()
    # 设置字符编码及自动提交
    cursor.execute('set names utf8')  # 固定格式
    cursor.execute('set autocommit=1')
    # 设置自动提交
    return conn, cursor


# 更新英雄使用频次
def saveHeroData(herodic):
    conn, cursor = getMysqlConn()
    # sql语句
    sql = "UPDATE t_lolHotHero SET	count = %s WHERE heroName = %s "
    for key, value in herodic.items():
        add_data = (value, key)
        # 这里会把sql语句中的%s替换掉
        cursor.execute(sql, add_data)
    print("mysql插入数据完成")
    # 关闭连接
    cursor.close()
    conn.close()


# 查询英雄数据
def searchHeroData():
    conn, cursor = getMysqlConn()
    # sql语句
    sql = "select heroName,count from t_lolHotHero"
    # 这里会把sql语句中的%s替换掉
    cursor.execute(sql)
    # 获取数据
    result = cursor.fetchall()
    print("mysql查询数据完成")
    # 关闭连接
    cursor.close()
    conn.close()
    return result


# 修改成为字典形式
def changeToDict(res):
    # 要返回的字典
    dict = {}
    for i in res:
        add_dict = {
            i[0]: i[1]
        }
        dict.update(add_dict)
    return dict


# 定时任务用（爬取一天的数据放进来好一点，否则数据量太小了）
def requestLOLHotHero():
    res = searchHeroData()
    herodict = changeToDict(res)
    artists = getInterData()
    herodic = getHero(artists, herodict)
    saveHeroData(herodic)


# 单元测试用
if __name__ == '__main__':
    requestLOLHotHero()
    # 数据库初始化
    # heroArr = [
    #     "格温", "腕豪瑟提", "铁男", "大虫子", "蒙多", "剑姬", "船长", "盖伦", "纳尔", "大头", "触手",
    #     "刀妹", "杰斯", "凯南", "克烈", "石头人", "大树", "猴子", "狗头", "泰坦", "奥拉夫", "奥恩",
    #     "潘森", "波比", "奎因", "鳄鱼", "兰博", "慎", "炼金", "赛恩", "蛤蟆", "提莫", "巨魔", "蛮王",
    #     "螃蟹", "狗熊", "掘墓", "佛耶戈", "莉莉娅", "木木", "青钢影", "蜘蛛", "寡妇", "稻草人", "酒桶",
    #     "男枪", "人马", "翠神", "皇子", "凯隐", "螳螂", "千珏", "剑圣", "豹女", "梦魇", "努努", "龙龟",
    #     "蝎子", "岩雀", "乌迪尔", "蔚", "狼人", "赵信", "扎克", "永恩", "奇亚娜", "佐伊", "狐狸",
    #     "阿卡丽", "冰鸟", "安妮", "龙王", "沙皇", "火男", "蛇女", "飞机", "皎月", "艾克", "小鱼人",
    #     "加里奥", "死歌", "卡萨丁", "卡特", "妖姬", "冰女", "拉克丝", "玛尔扎哈", "妮蔻", "发条", "瑞兹",
    #     "乌鸦", "辛德拉", "卡牌", "小法", "虚空之眼", "维克托", "吸血鬼", "泽拉斯", "炸弹人", "男刀",
    #     "VN", "莎弥拉", "厄斐琉斯", "卡莎", "寒冰", "女警", "德莱文", "EZ", "烬", "金克丝", "滑板鞋",
    #     "大嘴", "女枪", "轮子妈", "小炮", "韦鲁斯", "老鼠", "霞", "卢锡安", "芮尔", "萨勒芬妮", "赛娜",
    #     "悠米", "牛头", "巴德", "机器人", "布隆", "风女", "扇子妈", "日女", "璐璐", "莫甘娜", "娜美",
    #     "派克", "洛", "琴女", "星妈", "宝石", "时光", "婕拉", "锤石"
    # ]
    # for heroName in heroArr:
    #     saveAreaData(heroName)
